package net.j4love.spring.boot.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;

/**
 * spring 容器的事件监听器
 *
 * @author He Peng
 * @create 2017-05-19 11:50
 * @update 2017-05-19 11:50
 * @see
 */
public class ApplicationContextEventListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {

        doChoose(event);
    }

    private void doChoose(ApplicationEvent event) {

        if (event instanceof ContextClosedEvent) {
            doClosed((ContextClosedEvent) event);
        } else if (event instanceof ContextRefreshedEvent) {
            doRefreshed((ContextRefreshedEvent) event);
        } else if (event instanceof ContextStoppedEvent) {
            doStopped((ContextStoppedEvent) event);
        } else if (event instanceof ContextStartedEvent) {
            doStarted((ContextStartedEvent) event);
        }
    }

    private void doStarted(ContextStartedEvent event) {

        System.err.println("timestamp : " + event.getTimestamp() + "\r\n" +
                        "applicationContext started");
    }

    private void doStopped(ContextStoppedEvent event) {
        System.err.println("timestamp : " + event.getTimestamp() + "\r\n" +
                "applicationContext Stopped");
    }

    private void doRefreshed(ContextRefreshedEvent event) {
        System.err.println("timestamp : " + event.getTimestamp() + "\r\n" +
                "applicationContext Refreshed");
    }

    private void doClosed(ContextClosedEvent event) {
        System.err.println("timestamp : " + event.getTimestamp() + "\r\n" +
                "applicationContext Closed");
    }
}
